package com.example.lyl.testanimation;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener , Four.CloseActivity{

    Button alphaButton, scaleButton, translate_button, rotate_button, xubo_one,xubo_two,shanshuo,qiehuan,frame_anim,tiaozhuan;
    Animation animation;
    private ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        alphaButton = (Button) findViewById(R.id.alpha_button);
        image = (ImageView) findViewById(R.id.image);
        scaleButton = (Button) findViewById(R.id.scale_button);
        translate_button = (Button) findViewById(R.id.translate_button);
        rotate_button = (Button) findViewById(R.id.rotate_button);
        xubo_one = (Button) findViewById(R.id.xubo_one);
        xubo_two= (Button) findViewById(R.id.xubo_two);
        shanshuo= (Button) findViewById(R.id.shanshuo);
        qiehuan= (Button) findViewById(R.id.qiehuan);
        frame_anim= (Button) findViewById(R.id.frame_anim);
        tiaozhuan= (Button) findViewById(R.id.tiaozhuan);
        scaleButton.setOnClickListener(this);
        alphaButton.setOnClickListener(this);
        translate_button.setOnClickListener(this);
        rotate_button.setOnClickListener(this);
        xubo_one.setOnClickListener(this);
        xubo_two.setOnClickListener(this);
        shanshuo.setOnClickListener(this);
        qiehuan.setOnClickListener(this);
        frame_anim.setOnClickListener(this);
        tiaozhuan.setOnClickListener(this);

        SlefActivityManager.getInstance().addActivty(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.alpha_button:
                alphaImg();
                break;
            case R.id.scale_button:
                scaleImg();
                break;
            case R.id.translate_button:
                translate();
                break;
            case R.id.rotate_button:
                rotate();
                break;
            case R.id.xubo_one:
                xuboone();
                break;
            case R.id.xubo_two:
                xubotwo();
                break;
            case R.id.shanshuo:
                shan_shuo();
                break;
            case R.id.qiehuan:
                qie_huan();
                break;
            case R.id.frame_anim:
                frameanim();
                break;
            case R.id.tiaozhuan:
                startActivity(new Intent(this,SecondActivity.class));
                break;
            default:
                break;
        }
    }

    /**
     * 帧动画
     */
    private void frameanim() {
        AnimationDrawable drawable= (AnimationDrawable) image.getBackground();
        drawable.start();
    }

    /**
     * activity切换动画
     */
    private void qie_huan() {
        Intent intent=new Intent(this,SecondActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.in_anim,R.anim.out_anim);
    }

    /**
     * 闪烁动画
     */
    private void shan_shuo() {
        AlphaAnimation alphaAnimation=new AlphaAnimation(0.1f,1.0f);
        //重复次数
        alphaAnimation.setRepeatCount(10);
        //时间间隔
        alphaAnimation.setDuration(100);
        //设置倒叙播放还是正序播放
        alphaAnimation.setRepeatMode(Animation.REVERSE);
        image.startAnimation(alphaAnimation);
    }

    /**
     * 续播动画2
     */
    private void xubotwo() {
        animation = AnimationUtils.loadAnimation(this, R.anim.continue_anim);
        image.startAnimation(animation);
    }

    /**
     * 续播动画1
     */
    private void xuboone() {
        animation = AnimationUtils.loadAnimation(this, R.anim.translate);

        image.startAnimation(animation);
        final Animation animation1 = AnimationUtils.loadAnimation(this, R.anim.rotate);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            //上一个动画结束的时候
            @Override
            public void onAnimationEnd(Animation animation) {
                image.startAnimation(animation1);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

    }

    /**
     * 旋转动画
     */
    private void rotate() {
        animation = AnimationUtils.loadAnimation(this, R.anim.rotate);

        image.startAnimation(animation);
    }

    /**
     * 位移动画
     */
    private void translate() {
        animation = AnimationUtils.loadAnimation(this, R.anim.translate);

        image.startAnimation(animation);
    }

    /**
     * 缩放动画
     */
    private void scaleImg() {

        animation = AnimationUtils.loadAnimation(this, R.anim.scale);

        image.startAnimation(animation);

    }

    /**
     * 透明度渐变动画
     */
    private void alphaImg() {
        animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
        image.startAnimation(animation);
    }

    @Override
    public void finishActivity() {
        this.finish();
    }
}
